﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using EpiNEXT.Services.Fields;

namespace EpiNEXT.Services.Validation.Actions
{
    //[Serializable()]
    public sealed class Action_Assign : ActionBase
    {
        [XmlElement("Token", typeof(Token))]
        public Token AssignValue { get; set; }

        public override void Execute(Record record)
        {
            DataEntryInstrument form = record.Form;

            foreach (string fieldName in this.TargetNames)
            {
                if (form.Fields.Contains(fieldName))
                {
                    IField field = form.Fields[fieldName];
                    record.AddOrUpdateFieldData(field, AssignValue.GetValue(record));
                }
            }
        }

        public override List<IField> GetFieldsInAction(DataEntryInstrument form)
        {
            List<IField> fields = base.GetFieldsInAction(form);

            if (AssignValue != null)
            {
                fields.AddRange(AssignValue.GetFields(form));
            }

            return fields;
        }
    }
}
